CLANG=$(which clang)
PYTHON=$(which python)
RETVAL=0
if ! [[ -x $CLANG ]]; then
    echo -e "Can not found clang \033[1;31m[Error]\033[0m"
    RETVAL=1
fi
if ! [[ -x $PYTHON ]]; then
    echo -e "Can not found python \033[1;31m[Error]\033[0m"
    RETVAL=1
else
    PYTHON_EXECUTABLE=$(dirname $($PYTHON -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))"))/bin/python
fi

if [[ $RETVAL -eq 0 ]]; then
    rm /tmp/build -rf
    mkdir -p /tmp/build
    cd /tmp/build
    cmake -G "Unix Makefiles" \
        -DUSE_PYTHON2=OFF \
        -DUSE_SYSTEM_LIBCLANG=OFF  \
        -DPYTHON_INCLUDE_DIR=$($PYTHON_EXECUTABLE -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")  \
        -DPYTHON_LIBRARY=$($PYTHON_EXECUTABLE -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")/libpython3.so \
        -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
        . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

    cmake --build . --target ycm_core --config Release
    cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~
    cd -
fi
